# coding:utf-8
from flask import Flask, render_template
from flask_debugtoolbar import DebugToolbarExtension
from flask import abort,request,redirect,flash,url_for
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from wtforms.ext.i18n.form import Form
from wtforms import StringField, SubmitField, PasswordField
from wtforms.validators import Required, Length

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

app = Flask(__name__)

app.debug = True

app.config['SECRET_KEY'] = 'xybabc@*#lasfa'
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False

toolbar = DebugToolbarExtension(app)

bootstrap = Bootstrap(app)

moment = Moment(app)


class NameForm(Form):
    LANGUAGES = ['zh']
    name = StringField(u'你的姓名是?', validators=[Required()])
    password = PasswordField(u'密码', validators=[Required(), Length(min=6)])
    submit = SubmitField(u'提交')

@app.route('/', methods=['GET', 'POST'])
def hello_world():
    name = None
    form = NameForm(request.form)
    if request.method == 'POST' and form.validate():
        flash(u'呵呵，恭喜你注册成功')
        return redirect(url_for('user_agent'))
    return render_template('index.html', form=form, name=name)


@app.route('/user/<name>')
def user(name):
    return '<h1>hello, %s</h1>' % name


@app.route('/user-agent')
def user_agent():
    return render_template('list.html')


@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404


if __name__ == '__main__':
    app.run()
